package taller2.view.menus;

import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;

import javax.swing.JLabel;
import javax.swing.JPanel;

import taller2.utils.Point;
import taller2.utils.Utils;

public class HelpMenu extends JPanel{
	private static final long serialVersionUID = 1L;
	private int x,y,height;

	// constructor
	public HelpMenu(){
		setLayout(new GridBagLayout());
		x = 0;
		y = 0;
		height = 0;
		initializeLinks();
	}
	
	// initializes all links
	private void initializeLinks(){

		int aux = y;
		x = 0;

		addLabel(" ",x,y++);
		addLabel("General info:",x,y++);
		addLink("Graphs","http://en.wikipedia.org/wiki/Graph_(mathematics)",x,y++);
		addLink("Mit","http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/",x,y++);
		addLink("Mit(youtube)","https://www.youtube.com/watch?v=HtSuA80QTyo",x,y++);

		x = 1;
		y = aux;

		addLabel(" ",x,y++);
		addLabel("Topological search: \n",x,y++);
		addLink("info","http://en.wikipedia.org/wiki/Topological_sorting",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=rSCR8r2aNA8",x,y++);
		addLabel(" ",x,y++);

		x = 0;
		aux = y;

		addLabel(" ",x,y++);
		addLabel("DFS (Deep-First-Search):",x,y++);
		addLink("algorithm","http://en.wikipedia.org/wiki/Depth-first_search",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=bkROCj-BTWE",x,y++);

		x = 1;
		y = aux;

		addLabel(" ",x,y++);
		addLabel("BFS (Breadth-first-Search): \n",x,y++);
		addLink("algorithm","http://en.wikipedia.org/wiki/Breadth-first_search",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=we2xFCPkH0Y",x,y++);

		x = 0;
		aux = y;

		addLabel(" ",x,y++);
		addLabel("Cycles: \n",x,y++);
		addLink("info","http://en.wikipedia.org/wiki/Cycle_%28graph_theory%29",x,y++);
		addLink("algorithm","http://en.wikipedia.org/wiki/Cycle_detection",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=0tOXEoHN3Yc",x,y++);

		x = 1;
		y = aux;

		addLabel(" ",x,y++);
		addLabel("Strongly connected component: \n",x,y++);
		addLink("info","http://en.wikipedia.org/wiki/Strongly_connected_component",x,y++);
		addLink("algorithm","http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=J_Jl_r_Ua1Q",x,y++);

		x = 0;
		aux = y;

		addLabel(" ",x,y++);
		addLabel("Shortest path: \n",x,y++);
		addLink("info","http://en.wikipedia.org/wiki/Shortest_path_problem",x,y++);
		
		addLink("Dijkstra's algorithm","http://en.wikipedia.org/wiki/Dijkstra's_algorithm",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=gdmfOwyQlcI",x,y++);

		addLink("Floyd–Warshall's algorithm","http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=Qdt5WJVkPbY",x,y++);

		addLabel(" ",x,y++);
		addLabel("Transitive closure: \n",x,y++);
		addLink("info","http://en.wikipedia.org/wiki/Transitive_closure",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=UGiG4PoCC-E",x,y++);

		x = 1;
		y = aux;

		addLabel(" ",x,y++);
		addLabel("Minimum spamming tree: \n",x,y++);
		addLink("info","http://en.wikipedia.org/wiki/Minimum_spanning_tree",x,y++);
		addLink("Prim's algorithm","http://en.wikipedia.org/wiki/Prim%27s_algorithm",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=cplfcGZmX7I",x,y++);

		addLabel(" ",x,y++);
		addLabel(" ",x,y++);
		addLabel(" ",x,y++);
		addLabel("Maximum flow: \n",x,y++);
		addLink("info","http://en.wikipedia.org/wiki/Maximum_flow_problem",x,y++);
		addLink("Ford-Fulkerson's algorithm","http://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm",x,y++);
		addLink("Video","https://www.youtube.com/watch?v=_yOSku77w0o",x,y++);

	}
	
	
	private void addLabel(final String s, int x, int y){
		JLabel label = new JLabel(s);
		Utils.addComponent(this,label,1, 0, new Point(x,y));
	}

	private void addLink(final String s, final String link, int x, int y){
		JLabel label = makeHyperLink(s,link,0,height);
		Utils.addComponent(this,label,1, 0, new Point(x, y));
		height += 50;
	}

	/**
	 * not mine, all credit to:
	 * http://stackoverflow.com/questions/5703618/i-want-to-insert-hyperlink-in-jpanel
	 * @author Jonathan Leffler
	 */
    private static JLabel makeHyperLink(final String s, final String link, int x, int y){
    	final JLabel l = new JLabel(s);
    	l.addMouseListener(new MouseAdapter(){

    		@Override
        	public void mouseExited(MouseEvent arg0){
        		l.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        		l.setText(s);
        	}

        	@Override
        	public void mouseEntered(MouseEvent arg0){
        		l.setCursor(new Cursor(Cursor.HAND_CURSOR));
        		l.setText(String.format("<HTML><FONT color = \"#000099\"><U>%s</U></FONT></HTML>", s));
        	}

        	@Override
        	public void mouseClicked(MouseEvent arg0){
            	try{
            		URI uri = new URI(link);
                	if (Desktop.isDesktopSupported())
                		Desktop.getDesktop().browse(uri);
            	} catch (Exception e){
            	}
        	}
    	});

    	l.setBounds(x, y, s.length()*5, 20);
    	l.setToolTipText(String.format("go to %s", link));
    	return l;
    }
}
